import json
from builtins import list

from data_render import Recode
class FileReader:
    def  read_data(self) -> list[Recode]:
        pass

class TxtFileReader(FileReader):
    def __init__(self, filename: str):
       self.filename = filename

    def read_file(self) -> list[Recode]:
        f = open(self.filename, 'r',encoding="UTF-8")
        contents = f.read()
        recode_list = []
        for line in contents.splitlines():
            print(f"{line}-------line")
            line_data = line.split(',')
            recode_list.append(Recode(line_data[0], line_data[1], int(line_data[2]), line_data[3]))
        f.close()
        return recode_list


class JsonFileReader(FileReader):
    def __init__(self, filename: str):
        self.filename = filename
    def read_file(self) -> list[Recode]:
        f = open(self.filename, 'r',encoding="UTF-8")
        contents = f.read()
        recode_list = []
        for line_num,line in enumerate(contents.splitlines(), start=1):
            try:
                json_data = json.loads(line)
                print(type(json_data))
                recode_list.append(Recode(json_data["date"],json_data["order_id"],int(json_data["price"]),json_data["province"]))
            except json.decoder.JSONDecodeError as e:
                print(line_num,e)
        f.close()
        return recode_list
if __name__ == '__main__':
    # text = TxtFileReader('data.txt').read_file()
    # for r in text:
    #     r.__str__()
    jsonData = JsonFileReader("jsonData.txt").read_file()
    for recode in jsonData:
        recode.__str__()
